﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Material
{
    public partial class CreateMaterialView : UserControl, IView
    {
        public CreateMaterialView()
        {
            InitializeComponent();
        }
        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", "保存", btnSave_Click))
                .AddAction(new ActionNode("cancel", "取消", btnCancel_Click));
        }
        public void Resume(ViewResumeArgs args)
        {

        }
        public void Active(ViewActiveArgs args)
        {
            BindBrand();
            BindType();
            BindUnit();
            BindSalesCommissonWay();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region Method
        /// <summary>
        /// 绑定所属品牌
        /// </summary>
        private void BindBrand()
        {
            LookUpEditUtil.BindMasterData(this.lupBrand, Domain.Constant.MasterDataCodes.MaterialBrand, null, null);
        }
        /// <summary>
        /// 绑定所属类别
        /// </summary>
        private void BindType()
        {
            LookUpEditUtil.BindMasterDataWithClear(this.lupType, Domain.Constant.MasterDataCodes.MaterialCategory, null, null);
        }
        /// <summary>
        /// 绑定所属单位
        /// </summary>
        private void BindUnit()
        {
            LookUpEditUtil.BindMasterData(this.lupUnit, Domain.Constant.MasterDataCodes.MaterialUnit, null, null);
        }
        /// <summary>
        /// 绑定提成方式
        /// </summary>
        private void BindSalesCommissonWay()
        {
            this.lupCommissionWay.Properties.DataSource = Domain.Constant.CommissionTypes.Enumerate();
            this.lupCommissionWay.Properties.DisplayMember = "Text";
        }
        /// <summary>
        /// 数据检查
        /// </summary>
        private bool ValidateData()
        {
            if (string.IsNullOrEmpty(this.txtCode.Text))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入商品编码！");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入商品名称！");
                return false;
            }
            if (lupBrand.EditValue == null)
            {
                UIContext.Current.ViewManager.ShowWarning("请选择所属品牌！");
                return false;
            }
            if (lupType.EditValue == null)
            {
                UIContext.Current.ViewManager.ShowWarning("请选择所属分类！");
                return false;
            }
            if (this.lupUnit.EditValue == null)
            {
                UIContext.Current.ViewManager.ShowWarning("请选择商品单位！");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtRefCost.Text))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入参考成本！");
                return false;
            }
            else if (Convert.ToDecimal(this.txtRefCost.Text) < 0)
            {
                UIContext.Current.ViewManager.ShowWarning("参考成本小于0，请确认！");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtPrice.Text))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入销售价格！");
                return false;
            }
            else if (Convert.ToDecimal(this.txtPrice.Text) < 0)
            {
                UIContext.Current.ViewManager.ShowWarning("销售价格小于0，请确认！");
                return false;
            }
            if (this.lupCommissionWay.EditValue == null ||
                LookUpEditUtil.GetIntCode(lupCommissionWay) == 0)
            {
                UIContext.Current.ViewManager.ShowWarning("请选择提成方式！");
                return false;
            }
            else if (!string.IsNullOrEmpty(this.txtSalesCommissionAmount.Text.Trim())
                && LookUpEditUtil.GetIntCode(lupCommissionWay) != 1
                && Convert.ToDecimal(this.txtSalesCommissionAmount.Text) < 0)
            {
                UIContext.Current.ViewManager.ShowWarning("比例/金额小于0，请确认！");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 计算提成
        /// </summary>
        private void CaculateSalesMission()
        {
            //销售价格必须输入
            //提成方式选择的不是“全部”或 null
            if (!string.IsNullOrEmpty(this.txtPrice.Text.Trim())
                && this.lupCommissionWay.EditValue != null
                && LookUpEditUtil.GetIntCode(lupCommissionWay) != 0)
            {
                switch (LookUpEditUtil.GetIntCode(lupCommissionWay))
                {
                    case 1://不提成
                        this.txtSalesCommission.Text = "0";
                        break;
                    case 2://固定金额=固定金额
                        if (!string.IsNullOrEmpty(txtSalesCommissionAmount.Text.Trim())
                            && Convert.ToDecimal(txtSalesCommissionAmount.Text.Trim()) >= 0)
                        {
                            this.txtSalesCommission.Text = txtSalesCommissionAmount.Text.Trim();
                        }
                        break;
                    case 3://比例=价格*比例
                        if (!string.IsNullOrEmpty(txtSalesCommissionAmount.Text.Trim())
                            && Convert.ToDecimal(txtSalesCommissionAmount.Text.Trim()) >= 0)
                        {
                            this.txtSalesCommission.Text = ((Convert.ToDecimal(this.txtPrice.Text.Trim())) * (Convert.ToDecimal(txtSalesCommissionAmount.Text.Trim()) / 100)).ToString();
                        }
                        break;
                }
            }
        }
        /// <summary>
        /// 新增商品
        /// </summary>
        private void CreateMaterial()
        {
            WebRef.EMaterial model = new WebRef.EMaterial();
            model.Code = this.txtCode.Text.Trim();
            model.BusinessId = ModuleContext.Current.BusinessId;
            model.StationId = ModuleContext.Current.StationId;
            model.Name = this.txtName.Text.Trim();
            model.Price = Convert.ToDecimal(this.txtPrice.Text.Trim());
            model.Cost = Convert.ToDecimal(this.txtRefCost.Text.Trim());
            model.Category = LookUpEditUtil.GetSelectObject<WebRef.EMasterDataItem>(this.lupType).Code;
            model.Brand = this.lupBrand.EditValue.ToString();
            model.Unit = this.lupUnit.EditValue.ToString();
            model.QuickCode = this.txtFaseterCode.Text.Trim();
            model.Barcode = this.txtBarCode.Text.Trim();
            model.SalesCommission = Convert.ToDecimal(this.txtSalesCommission.Text.Trim());
            model.SalesCommissionType = LookUpEditUtil.GetIntCode(lupCommissionWay);
            //if (LookUpEditUtil.GetIntCode(lupCommissionWay) != 1)
            //{
            //    if (!string.IsNullOrEmpty(this.txtSalesCommissionAmount.Text.Trim()))
            //    {
            //        model.SalesCommissionAmount = Convert.ToDecimal(this.txtSalesCommissionAmount.Text.Trim());
            //    }
            //}
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                SingleResult<WebRef.EMaterial> result = new Service.CreateService().CreateMaterial(model);
                this.BeginInvoke(new Action<SingleResult<WebRef.EMaterial>>((p) =>
                {
                    this.HideMask();
                    if (p.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, p.Data);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(p.Message);
                    }
                }), result);
            });
        }

        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, object e)
        {
            try
            {
                if (ValidateData())
                {
                    CreateMaterial();
                }
            }
            catch (Exception ex)
            {
                UIContext.Current.ViewManager.ShowError(ex.Message);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 根据销售价格计算实际提成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPrice_EditValueChanged(object sender, EventArgs e)
        {
            CaculateSalesMission();
        }
        /// <summary>
        /// 根据提成方式计算实际提成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCommissionWay_EditValueChanged(object sender, EventArgs e)
        {
            CaculateSalesMission();
        }
        /// <summary>
        /// 根据比例/金额计算实际提成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSalesCommissionAmount_EditValueChanged(object sender, EventArgs e)
        {
            CaculateSalesMission();
        }
        #endregion
    }
}
